﻿using HCQ2_Common;
using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.Data;
using HCQ2_Model.VRDD.InputDto._20220627;
using HCQ2_Model.VRDD.OutputDto._20220627;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_BLL
{
    public partial class T_VRScoreSourceDetailBLL: IT_VRScoreSourceDetailBLL
    {
        /// <summary>
        ///  保存用户签到
        /// </summary>
        /// <param name="param"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public OperationResult<int?> SaveUserSign(InputSaveUserSign param, T_User user)
        {
            OperationResult<int?> result = new OperationResult<int?>(true);
            //0.0、参数定义
            bool isSign = true;// 是否正常签到
            DateTime signDate = DateTime.Now;//签到时间
            if (!string.IsNullOrEmpty(param.signDate))
            {
                isSign = false;
                signDate = DateTime.ParseExact(param.signDate, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None);
            }
            //签到前一天
            DateTime beforeSignDate = signDate.AddDays(-1);
            //前一天连续签到天数
            int? beforeDay = DBSession.IT_VRUserSignInDAL.FindSingle(s => s.userid.Equals(user.user_guid) && s.sigDate.ToString("yyyyMMdd").Equals(beforeSignDate.ToString("yyyyMMdd")))?.sigcomDay;
            //连续签到天数
            int sigcomDay= (null == beforeDay) ? 1 : Helper.ToInt(beforeDay) + 1;
            //1.0、保存签到记录
            int mark = DBSession.IT_VRUserSignInDAL.Add(new T_VRUserSignIn
            {
                sigCode = Helper.GetGuid,
                userid = user.user_guid,
                createId = user.user_id,
                createName = user.user_name,
                sigDate = signDate,
                sigcomDay = sigcomDay,
                isRewarded = isSign,
                proType = param.proType
            });
            if (mark <= 0)
            {
                result.Success = false;
                result.Message = "签到失败！";
                return result;
            }
            //2.0、保存积分来源明细
            //2.0.1、计算积分来源收入
            var score = DBSession.IT_VRScoreSourceDetailDAL.GetUserSignsCore();
            if (isSign)
            {
                var item = DBSession.IT_ItemCodeDAL.FindSingle(s => s.item_code.Equals("SignRewardRule"));
                if(null!=item && (string.IsNullOrEmpty(item.whereValue) || item.whereValue == "0"))
                {
                    //条件值 0 计算规则
                    int day = sigcomDay;
                    if (null != beforeDay)
                        day = sigcomDay - Helper.ToInt(beforeDay);
                    score += DBSession.IT_VRScoreSourceDetailDAL.GetUserSignsAwardCore(day);
                }
                else
                {
                    //条件值 1 计算规则
                    var obj = DBSession.IT_VRScoreSourceDetailDAL.GetMaxSigDayScore();
                    if (sigcomDay >= obj.sigDay)
                        score += obj.numValue;
                    else
                        score += DBSession.IT_VRScoreSourceDetailDAL.GetUserSignsAwardCore(sigcomDay);
                }
            } 
            //2.0.2、积分来源汇总记录
            var sourceMain = DBSession.IT_VRScoreSourceMainDAL.FindSingle(s => s.user_id == user.user_id && s.itemType.Equals("UserSign"));
            decimal inScore = score, _inScore = score, //收入合计
                outScore = 0, _outScore = 0, //支出合计
                totalScore = score, _totalScore = score;//余额
            if(null!= sourceMain)
            {
                inScore = sourceMain.inScore + score;
                outScore = sourceMain.outScore;
                totalScore = sourceMain.totalScore + score;
            }
            mark = DBSession.IT_VRScoreSourceDetailDAL.Add(new T_VRScoreSourceDetail
            {
                logCode = Helper.GetGuid,
                userid = user.user_guid,
                user_id = user.user_id,
                user_name = user.user_name,
                itemType = "UserSign",
                inScore = inScore,
                outScore = outScore,
                totalScore = totalScore,
                proType = param.proType,
                createDate = DateTime.Now,
                scoreNote = "用户签到"
            });
            //2.0.3、更新积分来源汇总
            if (mark > 0)
            {
                if (null != sourceMain)
                    //更新汇总
                    DBSession.IT_VRScoreSourceMainDAL.Modify(new T_VRScoreSourceMain { inScore = inScore, outScore = outScore, totalScore = totalScore }, s => s.logCode.Equals(sourceMain.logCode), "inScore", "outScore", "totalScore");
                else
                    DBSession.IT_VRScoreSourceMainDAL.Add(new T_VRScoreSourceMain
                    {
                        logCode = Helper.GetGuid,
                        userid = user.user_guid,
                        user_id = user.user_id,
                        user_name = user.user_name,
                        itemType = "UserSign",
                        proType = param.proType,
                        inScore = inScore,
                        outScore = outScore,
                        totalScore = totalScore,
                        scoreNote = "用户签到",
                        createDate = DateTime.Now,
                        updateDate = DateTime.Now
                    });
            }
            //3.0、保存积分明细
            //3.0.1、积分汇总记录
            var totalMain = DBSession.IT_VRTotalScoreMainDAL.FindSingle(s => s.user_id == user.user_id);
            if(null!= totalMain)
            {
                _inScore = totalMain.inGold + score;
                _outScore = totalMain.outGold;
                _totalScore = totalMain.totalGold + score;
            }
            mark = DBSession.IT_VRTotalScoreDetailDAL.Add(new T_VRTotalScoreDetail {
                logCode = Helper.GetGuid,
                userid = user.user_guid,
                user_id = user.user_id,
                user_name = user.user_name,
                itemType = "UserSign",
                inGold = _inScore,
                outGold = _outScore,
                totalGold = _totalScore,
                proType = param.proType,
                createDate = DateTime.Now,
                scoreNote = "用户签到"
            });
            //3.0.2、更新积分汇总
            if (mark > 0)
            {
                if (null != totalMain)
                    DBSession.IT_VRTotalScoreMainDAL.Modify(new T_VRTotalScoreMain { inGold = _inScore, outGold = _outScore, totalGold = _totalScore }, s => s.logCode.Equals(totalMain.logCode), "inGold", "outGold", "totalGold");
                else
                    DBSession.IT_VRTotalScoreMainDAL.Add(new T_VRTotalScoreMain
                    {
                        logCode = Helper.GetGuid,
                        userid = user.user_guid,
                        user_id = user.user_id,
                        user_name = user.user_name,
                        proType = param.proType,
                        inGold = _inScore,
                        outGold = _outScore,
                        totalGold = _totalScore
                    });
            }
            //查询最近签到时间
            var SignInfo = DBSession.IT_VRUserSignInDAL.Select(o => o.userid == param.userid && o.proType == param.proType, order => order.sigDate, false);
            DateTime SignDate = SignInfo[0].sigDate;
            int dayNum = DBSession.IT_ItemCodeMenumDAL.GetSignInNum(SignDate, user.user_id).sigcomDay;
            result.Data = dayNum;
            return result;
        }
        /// <summary>
        ///  获取积分规则指定代码奖励
        /// </summary>
        /// <param name="codeValue"></param>
        /// <returns></returns>
        public AwardParam GetAwardByCode(string codeValue)
        {
            AwardParam result = new AwardParam();
            var obj = DBSession.IT_VRScoreSourceDetailDAL.GetAwardByCode(codeValue);
            if(null==obj)
                return result;
            int whereValue = 0, numValue = 0;
            if (!string.IsNullOrEmpty(obj.whereValue))
                int.TryParse(obj.whereValue,  out whereValue);
            if (!string.IsNullOrEmpty(obj.numValue))
                int.TryParse(obj.numValue, out numValue);
            result.whereValue = whereValue;
            result.numValue = numValue;
            return result;
        }
    }
}
